﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Web;

namespace LeadVinaglue.Helpers
{
    public class XmlHelper
    {
        public static void SerializeObject(string filename, System.Type oType, object objectToSerialize)
        {
            TextWriter textWriter = null;
            try
            {
                XmlSerializer serializer = new XmlSerializer(oType);
                textWriter = new StreamWriter(filename);
                serializer.Serialize(textWriter, objectToSerialize);
            }
            finally
            {
                if (textWriter != null)
                    textWriter.Close();
            }
        }

        public static object DeSerializeObject(string filename, System.Type oType)
        {
            TextReader textReader = null;
            object oBatch;
            try
            {
                XmlSerializer deserializer = new XmlSerializer(oType);
                textReader = new StreamReader(filename);

                oBatch = deserializer.Deserialize(textReader);
            }
            finally
            {
                if (textReader != null)
                    textReader.Close();
            }

            return oBatch;
        }

        public static string SerializeObjectToString(System.Type oType, object objectToSerialize)
        {
            TextWriter textWriter = null;
            MemoryStream oMemoryStream = new MemoryStream();
            try
            {
                XmlSerializer serializer = new XmlSerializer(oType);
                textWriter = new StreamWriter(oMemoryStream);
                serializer.Serialize(textWriter, objectToSerialize);
                return System.Text.Encoding.UTF8.GetString(oMemoryStream.ToArray());
            }
            finally
            {
                if (textWriter != null)
                    textWriter.Close();
            }
        }

        public static object DeSerializeObjectFromString(string value, System.Type oType)
        {
            TextReader textReader = null;
            object oBatch;
            try
            {
                XmlSerializer deserializer = new XmlSerializer(oType);
                textReader = new StringReader(value);

                oBatch = deserializer.Deserialize(textReader);
            }
            finally
            {
                if (textReader != null)
                    textReader.Close();
            }

            return oBatch;
        }

        public static object DeSerializeObject(byte[] arrByte, System.Type oType)
        {
            object oReturn = null;
            using (MemoryStream stream = new MemoryStream(arrByte))
            {
                XmlSerializer serializer = new XmlSerializer(oType);
                oReturn = serializer.Deserialize(stream);
                stream.Close();
            }

            return oReturn;
        }
    }
}
